package cn.hengzhu.main_manager.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.api.response.AlipayTradeRefundResponse;

import cn.hengzhu.main_manager.repository.dao.CustomersAmountMapper;
import cn.hengzhu.main_manager.repository.dao.CustomersRefundMapper;
import cn.hengzhu.main_manager.repository.dao.PayOrderMapper;
import cn.hengzhu.main_manager.repository.domain.CustomersAmount;
import cn.hengzhu.main_manager.repository.domain.CustomersAmountExample;
import cn.hengzhu.main_manager.repository.domain.CustomersRefund;
import cn.hengzhu.main_manager.repository.domain.PayOrder;
import cn.hengzhu.main_manager.service.CustomersAmountService;

@Service
@Transactional(rollbackFor = Exception.class)
public class CustomersAmountServiceImpl implements CustomersAmountService {

	@Autowired
	private CustomersAmountMapper customersAmountMapper;
	@Autowired
	private CustomersRefundMapper customersRefundMapper;
	@Autowired
	private PayOrderMapper payOrderMapper;

	@Override
	public void backPayByAli(AlipayTradeRefundResponse response, CustomersAmount customersAmount) {

		// 1.退款成功，修改押金状态
		Integer amountId = customersAmount.getAmountId();
		CustomersAmount record = new CustomersAmount();
		record.setIsPay((byte) 0);
		record.setAmountId(amountId);
		record.setAssureAmount(BigDecimal.ZERO);// 清空押金
		customersAmountMapper.updateByPrimaryKeySelective(record);
		// 2.在退款的表中插入一条数据
		String tradeNo = response.getTradeNo();// 退款前支付订单号

		String refundFee = response.getRefundFee();// 总金额
		String sendBackFee = response.getSendBackFee();// 退款金额
		Double valueOf = (Double.valueOf(refundFee)) * 100;
		Double valueOf2 = (Double.valueOf(sendBackFee)) * 100;

		CustomersRefund customersRefund = new CustomersRefund();
		customersRefund.setCreateTime(new Date());
		customersRefund.setOutRefundNo("支付宝退款未生成订单号");
		customersRefund.setRefundId("支付宝退款未生成订单号");
		customersRefund.setRefundFee(valueOf2.intValue());
		customersRefund.setTotalFee(valueOf.intValue());
		customersRefund.setTransactionId(tradeNo);
		customersRefundMapper.insertSelective(customersRefund);
	}

	/**
	 * @author liuCheng
	 * @since 2018年11月8日 下午1:56:54
	 * @param payOrder
	 *            支付宝小程序的充值逻辑（异步通知）
	 */
	@Override
	public void rechargeFinishByAliXCX(PayOrder payOrder) {

		Integer orderId = payOrder.getOrderId();
		Integer totalFee = payOrder.getTotalFee();
		String openId = payOrder.getOpenId();
		PayOrder updatePayOrder = new PayOrder();
		updatePayOrder.setOrderId(orderId);
		updatePayOrder.setIsPay((byte) 1);
		payOrderMapper.updateByPrimaryKeySelective(updatePayOrder);
		// 3.修改用户资金账户余额
		CustomersAmountExample example1 = new CustomersAmountExample();
		example1.createCriteria().andOpenIdEqualTo(openId);
		List<CustomersAmount> selectByExample2 = customersAmountMapper.selectByExample(example1);
		if (!selectByExample2.isEmpty()) {
			CustomersAmount customersAmount = selectByExample2.get(0);
			double amout = new BigDecimal(totalFee).divide(new BigDecimal(100), 3, BigDecimal.ROUND_HALF_UP)
					.doubleValue();
			BigDecimal totalAmount = customersAmount.getTotalAmount();
			double doubleValue = totalAmount.doubleValue();
			double newAmount = doubleValue + amout;
			CustomersAmount record = new CustomersAmount();
			record.setTotalAmount(BigDecimal.valueOf(newAmount));
			record.setAmountId(customersAmount.getAmountId());
			customersAmountMapper.updateByPrimaryKeySelective(record);
		}
	}

	/**
	 * 清空押金
	 * @param openId
	 */
	@Override
	public void clearPledge(String openId) {
		CustomersAmountExample example = new CustomersAmountExample();
		example.createCriteria().andOpenIdEqualTo(openId);
		List<CustomersAmount> list = customersAmountMapper.selectByExample(example);
		if (!list.isEmpty()) {
			CustomersAmount ca = list.get(0);
			ca.setAssureAmount(BigDecimal.ZERO); // 押金设置为0
			ca.setIsPay((byte) 0); // 是否缴纳押金改为否
			customersAmountMapper.updateByPrimaryKey(ca);
		}
	}

	/**
	 * 设置押金
	 * @param openId
	 * @param pledge
	 */
	@Override
	public void setPledge(String openId, BigDecimal pledge) {
		CustomersAmountExample example = new CustomersAmountExample();
		example.createCriteria().andOpenIdEqualTo(openId);
		List<CustomersAmount> list = customersAmountMapper.selectByExample(example); // from customers_amount
		if (!list.isEmpty()) {
			CustomersAmount ca = list.get(0);
			ca.setAssureAmount(pledge);
			ca.setIsPay((byte) 1);
			customersAmountMapper.updateByPrimaryKey(ca); //	update customers_amount
		}
	}

	@Override
	public CustomersAmount isOwe(String openId) {
		return customersAmountMapper.selectByOpenid(openId);
	}

}
